﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using Water.Model;
using WaterApp.Common;

namespace WaterApp.Business
{
    public partial class ExplosionAccidentDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lnkQuery_Click(object sender, EventArgs e)
        {
            string url = WebapiUrl.weburl() + "GetExplosionAccidentDetail/" + tbmrno.Text;
            var httpClient = new HttpClient();
            var responseJson = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            Water.Model.ExplosionAccidentDetail wmead = JsonConvert.DeserializeObject<Water.Model.ExplosionAccidentDetail>(responseJson);
            if (wmead.MRNO != null)
            {
                //hfmrno.Value = svopq.MRNO;
                //tbmrno.Text = 
                tbbzdt.Text = wmead.PipeBzDt.ToString();
                tbpiped.Text = wmead.PIPED.ToString();
                tbbzaddress.Text = wmead.BzAddress;
                tbtemp.Text = wmead.Temperature.ToString();
                tbinsdate.Text = wmead.INSDATE.ToString();
                tbdepth.Text = wmead.DEPTH.ToString();
                tbvalveqbqk.Text = wmead.ValveQk;
                tbdqshuiya.Text = wmead.AreaShuiYa.ToString();
                tbgcmade.Text = wmead.MadeUnit;
                tbmadedt.Text = wmead.MadeDt.ToString();
                tbmadefsh.Text = wmead.MadeFsh;
                SetCheckBoxList(cbljktl, wmead.JkTianliao);
                SetCheckBoxList(cblffcl, wmead.FangfuCl);
                tbbzcount.Text = wmead.BzCount.ToString();
                SetCheckBoxList(cblroadtype, wmead.RoadType);
                SetCheckBoxList(cbldztr, wmead.RoadSoil);
                SetCheckBoxList(cbldshhj, wmead.RoadUpEnvir);
                SetCheckBoxList(cbljtfh, wmead.JiaoTongFh);
                tbdxshuiwei.Text = wmead.DxiaShuiW.ToString();
                tbtrphvalue.Text = wmead.PHValue.ToString();
                tbxiufudt.Text = wmead.XfDT.ToString();
                tbworkunit.Text = wmead.WorkUnit;
                tbusermans.Text = wmead.UserMans;
                tbnearsgqk.Text = wmead.NearQk;
                tbsuishiqk.Text = wmead.LossInfo;
                tbloushisl.Text = wmead.LossWater.ToString();
                tbbzreason.Text = wmead.BzReason;
                tbbzyxfw.Text = wmead.YingXiangFw;
                //fileup1 = wmead.Image1Url;
                //fileup2.FileName = wmead.Image2Url;
            }
        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            string url = WebapiUrl.weburl() + "EditExplosionAccidentDetail";
            Water.Model.ExplosionAccidentDetail wmead = new Water.Model.ExplosionAccidentDetail()
            {
                MRNO = tbmrno.Text,
                PipeBzDt = Convert.ToDateTime(tbbzdt.Text),
                PIPED = Convert.ToSingle(tbpiped.Text),
                BzAddress = tbbzaddress.Text,
                Temperature = Convert.ToDouble(tbtemp.Text),
                INSDATE = Convert.ToDateTime(tbinsdate.Text),
                DEPTH = Convert.ToSingle(tbdepth.Text),
                ValveQk = tbvalveqbqk.Text,
                AreaShuiYa = Convert.ToSingle(tbdqshuiya.Text),
                MadeUnit = tbgcmade.Text,
                MadeDt = Convert.ToDateTime(tbmadedt.Text),
                MadeFsh = tbmadefsh.Text,
                JkTianliao = GetCheckBoxListSelValue(cbljktl),
                FangfuCl = GetCheckBoxListSelValue(cblffcl),
                BzCount = Convert.ToInt32(tbbzcount.Text),
                RoadType = GetCheckBoxListSelValue(cblroadtype),
                RoadSoil = GetCheckBoxListSelValue(cbldztr),
                RoadUpEnvir = GetCheckBoxListSelValue(cbldshhj),
                JiaoTongFh = GetCheckBoxListSelValue(cbljtfh),
                DxiaShuiW = Convert.ToDouble(tbdxshuiwei.Text),
                PHValue = Convert.ToDouble(tbtrphvalue.Text),
                XfDT = Convert.ToDateTime(tbxiufudt.Text),
                WorkUnit = tbworkunit.Text,
                UserMans = tbusermans.Text,
                NearQk = tbnearsgqk.Text,
                LossInfo = tbsuishiqk.Text,
                LossWater = Convert.ToDouble(tbloushisl.Text),
                BzReason = tbbzreason.Text,
                YingXiangFw = tbbzyxfw.Text,
                Image1Url = fileup1.FileName,
                Image2Url = fileup2.FileName
            };
            var requestJson = JsonConvert.SerializeObject(wmead);
            HttpContent httpContent = new StringContent(requestJson);

            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var httpClient = new HttpClient();
            var responseJson = httpClient.PostAsync(url, httpContent)
                .Result.Content.ReadAsStringAsync().Result;
            if (responseJson == "true")
            {
                lbmessage.Text = "保存成功!";
            }
            else
            {
                lbmessage.Text = "保存时发生异常！";
            }
        }

        private string GetCheckBoxListSelValue(CheckBoxList cbl)
        {
            string retstr = "";
            StringBuilder strbul = new StringBuilder();
            foreach (ListItem li in cbl.Items)
            {
                if (li.Selected)
                {
                    strbul.Append(li.Value + "|");
                }
            }
            if (strbul.Length > 0)
            {
                retstr = strbul.ToString().Substring(0, strbul.ToString().Length - 1);
            }
            return retstr;
        }

        private void SetCheckBoxList(CheckBoxList cbl, string selvalue)
        {
            string[] selv = selvalue.Split('|');
            for (int n = 0; n < selv.Length; n++)
            {
                foreach (ListItem li in cbl.Items)
                {
                    if (li.Value == selv[n])
                    {
                        li.Selected = true;
                    }
                }
            }
        }
    }
}